/**
 * Project Name:mauto-web
 * File Name:ApiJsOrderController.java
 * Package Name:com.inftech.mauto.jy.web.jsPad
 * Date:2017年6月19日上午9:43:56
 * Copyright (c) 2017, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.jy.web.jsPad;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.inftech.mauto.common.base.BaseController;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.jy.entity.JyOrder;
import com.inftech.mauto.jy.service.JyOrderService;
import com.inftech.mauto.jy.service.TechnicianService;

/**
 * ClassName:ApiJsOrderController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Date: 2017年6月19日 上午9:43:56 <br/>
 * 
 * @author Administrator
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping(value = "/api/js/order", produces = "application/json;charset=utf-8")
@Transactional(rollbackFor = Exception.class)
public class ApiJsOrderController extends BaseController {
	@Resource
	private JyOrderService jyOrderServiceImpl;

	@Resource(name = "technicianBizImpl")
	private TechnicianService technicianServiceImpl;

	/**
	 * 
	 * queryOrderList:(这里用一句话描述这个方法的作用). <br/>
	 * status 0 是进行中 1 是完成
	 * 
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/queryOrderList")
	@ResponseBody
	public String queryOrderList(HttpServletRequest request, HttpServletResponse response) {
		String token = request.getParameter("token");
		String status = request.getParameter("status");
		String pageSize = request.getParameter("pageSize");
		String pageNo = request.getParameter("pageNo");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("token", token);
		map.put("status", status);
		map.put("pageSize", Integer.parseInt(pageSize));
		map.put("pageNo", pageNo);
		map.put("limit", (Integer.parseInt(pageNo) - 1) * Integer.parseInt(pageSize));
		List<Map<String, Object>> MapList = new ArrayList<Map<String, Object>>();
		List<JyOrder> jyOrderList = new ArrayList<JyOrder>();
		int count = 0;
		if ("0".equals(status)) {
			jyOrderList = jyOrderServiceImpl.queryOrderList(map);
			count = jyOrderServiceImpl.queryOrderCount(map);
		}
		if ("1".equals(status)) {
			jyOrderList = jyOrderServiceImpl.queryOrderList1(map);
			count = jyOrderServiceImpl.queryOrderCount(map);
		}
		for (JyOrder j : jyOrderList) {
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("orderCode", j.getOrderCode());
			map2.put("carNo", j.getCarNo());
			map2.put("time", j.getTime());
			MapList.add(map2);
		}
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("count", count);
		resultMap.put("list", MapList);
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), resultMap);
		return outJson;
	}

	/**
	 * 
	 * searchOrderList:(订单编号查询). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/searchOrderList")
	@ResponseBody
	public String searchOrderList(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("searchStr") == null ? "" : request.getParameter("searchStr").trim();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// map.put("token", token);
		if ("".equals(orderCode)) {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_420;
			String outJson = getOutJson1(statusCode, false, statusCode.toString(), null);
			return outJson;
		}
		map.put("orderCode", orderCode);
		List<JyOrder> jyOrderList = jyOrderServiceImpl.searchOrderList(map);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		List<Map<String, Object>> MapList = new ArrayList<Map<String, Object>>();
		for (JyOrder j : jyOrderList) {
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("orderCode", j.getOrderCode());
			map2.put("carNo", j.getCarNo());
			map2.put("createTime", j.getCancelTime());
			MapList.add(map2);
		}
		resultMap.put("list", MapList);
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), resultMap);
		return outJson;
	}

	/**
	 * 
	 * getOrderDetail:(加载订单详情). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/getOrderDetail")
	@ResponseBody
	public String getOrderDetail(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode") == null ? "" : request.getParameter("orderCode").trim();
		// map.put("token", token);
		map.put("orderCode", orderCode);
		JyOrder jyOrder = jyOrderServiceImpl.getOrderDetail(map);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (jyOrder != null) {
			resultMap.put("orderCode", jyOrder.getOrderCode());
			resultMap.put("yyTime", jyOrder.getYyTime());
			resultMap.put("yyAddress", jyOrder.getYyAddress());
		}
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), resultMap);
		return outJson;
	}

	/**
	 * 
	 * getCarDetail:(加载车辆信息). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "getCarDetail")
	@ResponseBody
	public String getCarDetail(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode") == null ? "" : request.getParameter("orderCode").trim();
		// map.put("token", token);
		map.put("orderCode", orderCode);
		JyOrder jyOrder = jyOrderServiceImpl.getCarDetail(map);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (jyOrder != null) {
			resultMap.put("contactName", jyOrder.getContactName());
			resultMap.put("contactPhone", jyOrder.getContactPhone());
			resultMap.put("carNo", jyOrder.getCarNo());
			resultMap.put("carXh", jyOrder.getCarXh());
			resultMap.put("carCjh", jyOrder.getCarCjh());
			resultMap.put("carFdjbh", jyOrder.getCarFdjbh());
			resultMap.put("carXslc", jyOrder.getCarXslc());
			resultMap.put("xcbylc", jyOrder.getXcbylc());
			resultMap.put("xcbyDate", jyOrder.getXcbyDate());
		}
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), resultMap);
		return outJson;
	}

	/**
	 * 
	 * saveCarDetail:(保存车辆信息). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/saveCarDetail")
	@ResponseBody
	public String saveCarDetail(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode") == null ? "" : request.getParameter("orderCode").trim();
		String carCjh = request.getParameter("carCjh") == null || "".equals(request.getParameter("carCjh")) ? null
				: request.getParameter("carCjh").trim();
		String carFdjbh = request.getParameter("carFdjbh") == null || "".equals(request.getParameter("carFdjbh")) ? null
				: request.getParameter("carFdjbh").trim();
		String carXslc = request.getParameter("carXslc") == null || "".equals(request.getParameter("carXslc")) ? null
				: request.getParameter("carXslc").trim();
		String xcbylc = request.getParameter("xcbylc") == null || "".equals(request.getParameter("xcbylc")) ? null
				: request.getParameter("xcbylc").trim();
		String xcbyDate = request.getParameter("xcbyDate") == null || "".equals(request.getParameter("xcbyDate")) ? null
				: request.getParameter("xcbyDate").trim();
		// map.put("token", token);
		map.put("orderCode", orderCode);
		map.put("carCjh", carCjh);
		map.put("carFdjbh", carFdjbh);
		map.put("carXslc", carXslc);
		map.put("xcbylc", xcbylc);
		map.put("xcbyDate", xcbyDate);
		int count = jyOrderServiceImpl.saveCarDetail(map);
		String outJson = "";
		if (count == 1) {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
		} else {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_400;
			outJson = getOutJson1(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * getWxxmDetail:(加载维修项目详情). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/getWxxmDetail")
	@ResponseBody
	public String getWxxmDetail(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode") == null ? "" : request.getParameter("orderCode").trim();
		// map.put("token", token);
		map.put("orderCode", orderCode);
		List<Map<String, Object>> wxxmDetailList = jyOrderServiceImpl.getWxxmDetail(map);

		// orderTcIdList列表
		List<String> orderTcIdList = new ArrayList<String>();
		// 存放套餐
		List<Map<String, Object>> tcList = new ArrayList<Map<String, Object>>();

		for (Map<String, Object> resultMap : wxxmDetailList) {
			if (resultMap.get("orderTcId") != null) {
				if (!orderTcIdList.contains(resultMap.get("orderTcId").toString())) {
					orderTcIdList.add(resultMap.get("orderTcId").toString());
				}
			}
		}
		for (String orderTcId : orderTcIdList) {
			Map<String, Object> map1 = new HashMap<String, Object>();
			// 套餐内项目
			List<Map<String, Object>> tcxmList = new ArrayList<Map<String, Object>>();
			for (Map<String, Object> resultMap : wxxmDetailList) {
				if (resultMap.get("orderTcId") != null && orderTcId.equals(resultMap.get("orderTcId").toString())) {
					// 存放套餐
					// 套餐内项目list 里面的成员
					Map<String, Object> tcxmMap = new HashMap<String, Object>();
					// map1.put("orderTcId", resultMap.get("orderTcId"));
					map1.put("tcName", resultMap.get("tcName"));
					map1.put("tcJmfy", resultMap.get("tcJmfy"));
					map1.put("tcPrice", resultMap.get("tcPrice"));
					map1.put("isBzdjm", resultMap.get("isBzdjm"));
					tcxmMap.put("xmName", resultMap.get("xmName"));
					tcxmMap.put("xmprice", resultMap.get("tcPrice"));
					tcxmMap.put("pjlb", resultMap.get("pjlb"));
					tcxmMap.put("pjCode", resultMap.get("pjCode"));
					tcxmMap.put("pjpp", resultMap.get("pjpp"));
					tcxmMap.put("pjName", resultMap.get("pjName"));
					tcxmMap.put("pjPrice", resultMap.get("pjPrice"));
					tcxmMap.put("pjNum", resultMap.get("pjNum"));
					tcxmMap.put("isZd", resultMap.get("isZd"));
					tcxmList.add(tcxmMap);
				}
			}
			map1.put("tcxmList", tcxmList);
			tcList.add(map1);
		}
		// 存放项目
		List<Map<String, Object>> xmList = new ArrayList<Map<String, Object>>();

		for (Map<String, Object> resultMap : wxxmDetailList) {
			// 存放项目
			Map<String, Object> map2 = new HashMap<String, Object>();
			if (resultMap.get("orderTcId") == null) {
//				map2.put("tcName", resultMap.get("tcName"));
				// map2.put("tcJmfy", resultMap.get("tcJmfy"));
				// map2.put("tcPrice", resultMap.get("tcPrice"));
				map2.put("xmName", resultMap.get("xmName"));
				map2.put("xmprice", resultMap.get("tcPrice"));
				map2.put("pjlb", resultMap.get("pjlb"));
				map2.put("pjCode", resultMap.get("pjCode"));
				map2.put("pjpp", resultMap.get("pjpp"));
				map2.put("pjName", resultMap.get("pjName"));
				map2.put("pjPrice", resultMap.get("pjPrice"));
				map2.put("pjNum", resultMap.get("pjNum"));
				map2.put("isZd", resultMap.get("isZd"));
				xmList.add(map2);
			}

		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("xmList", xmList);
		resultMap.put("tcList", tcList);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), resultMap);

		return outJson;
	}

	/**
	 * 
	 * getAllWxxm:(加载所有套餐项目配件列表). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/getAllWxxm")
	@ResponseBody
	public String getAllWxxm(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		List<Map<String, Object>> tcList = jyOrderServiceImpl.getTcList();
		List<Map<String, Object>> xmList = jyOrderServiceImpl.getXmList();
		List<Map<String, Object>> pjList = jyOrderServiceImpl.getPjList();
		List<Map<String, Object>> resultTcList = new ArrayList<Map<String, Object>>();
		// 少配件信息
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("xmList", xmList);
		Set<String> set = new LinkedHashSet<String>();
		for (Map<String, Object> map1 : tcList) {
			if (!set.contains(map1.get("id").toString())) {
				set.add(map1.get("id").toString());
			}
		}

		for (String str : set) {
			Map<String, Object> resultMap1 = new HashMap<String, Object>();
			List<Map<String, Object>> tcxmList = new ArrayList<Map<String, Object>>();
			for (Map<String, Object> map1 : tcList) {
				if (str.equals(map1.get("id").toString())) {
					Map<String, Object> tcxmMap = new HashMap<String, Object>();
					// map1.put("orderTcId", resultMap.get("orderTcId"));
					resultMap1.put("tcName", map1.get("tcName"));
					resultMap1.put("tcJmfy", map1.get("tcJmfy"));
					resultMap1.put("tcPrice", map1.get("tcPrice"));
					resultMap1.put("isBzdjm", map1.get("isBzdjm"));
					tcxmMap.put("xmName", map1.get("xmName"));
					tcxmMap.put("xmprice", map1.get("tcPrice"));
					tcxmMap.put("pjlb", map1.get("pjlb"));
					tcxmMap.put("isZd", map1.get("isZd"));
					tcxmList.add(tcxmMap);
				}
			}
			resultMap1.put("tcxmList", tcxmList);

			resultTcList.add(resultMap1);
		}

		resultMap.put("tcList", resultTcList);
		resultMap.put("pjList", pjList);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), resultMap);
		return outJson;
	}

	/**
	 * 
	 * saveWxxm:(保存维修项目). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/saveWxxm")
	@ResponseBody
	public String saveWxxm(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode") == null ? "" : request.getParameter("orderCode").trim();
		map.put("orderCode", orderCode);
		String gsf = jyOrderServiceImpl.getWxxmPrice("工时费");
		String smfwf = jyOrderServiceImpl.getWxxmPrice("上门服务费");
		// 费用
		String fy = null;
		Integer orderId = jyOrderServiceImpl.getOrderIdByOrderCode(orderCode);
		Integer jsId = technicianServiceImpl.getJSIdByToken(token);
		String jsonStr = request.getParameter("jsonStr");
		JSONObject jsonObject = JSONObject.parseObject(jsonStr);
		List<Map> tcList = JSONArray.parseArray(JSONObject.toJSONString(jsonObject.get("tcList")), Map.class);
		List<Map> xmList = JSONArray.parseArray(JSONObject.toJSONString(jsonObject.get("xmList")), Map.class);
		jyOrderServiceImpl.updateOrderTc(map);
		jyOrderServiceImpl.updateOrderWxxm(map);
		jyOrderServiceImpl.updateOrderPj(map);
		double tcxmFy = 0;
		double tcjmFy = 0;
		boolean flag = false;
		int count = 0;
		// 订单优惠
		double orderYhPrice = 0;
		for (Map<Object, Object> tcMap : tcList) {
			tcMap.put("orderId", orderId);
			tcMap.put("createBy", jsId);
			tcMap.put("tcId", "");
			tcjmFy += Double.parseDouble(tcMap.get("tcJmfy") == null || "".equals(tcMap.get("tcJmfy").toString()) ? "0"
					: tcMap.get("tcJmfy").toString());
			jyOrderServiceImpl.insertJyOrderTc(tcMap);
			for (Map map1 : JSONArray.parseArray(JSONObject.toJSONString(tcMap.get("tcxmList")), Map.class)) {
				map1.put("tcId", tcMap.get("tcId"));
				map1.put("orderId", orderId);
				map1.put("createBy", jsId);
				if (map1.get("isZd") != null && "1".equals(map1.get("isZd").toString())) {
					BigDecimal bigDecimal = new BigDecimal(
							map1.get("pjPrice") == null || "".equals(map1.get("pjPrice").toString()) ? "0"
									: map1.get("pjPrice").toString());
					BigDecimal bigDecimal1 = new BigDecimal(
							map1.get("pjNum") == null || "".equals(map1.get("pjNum").toString()) ? "0"
									: map1.get("pjNum").toString());
					tcxmFy += bigDecimal.multiply(bigDecimal1).doubleValue();
				}
				tcxmFy += Double.parseDouble(map1.get("xmprice") == null || "".equals(map1.get("xmprice").toString())
						? "0" : map1.get("xmprice").toString());
				map1.put("wxxmid", "");
				jyOrderServiceImpl.insertJyOrderWxxmWithTcId(map1);
				jyOrderServiceImpl.insertJyOrderPj(map1);
			}
			// 判断是是不是可自带套餐
			if (tcMap.get("is_bzdjm") != null && "0".equals(tcMap.get("is_bzdjm").toString())) {
				flag = true;
			}
			for (Map map1 : JSONArray.parseArray(JSONObject.toJSONString(tcMap.get("tcxmList")), Map.class)) {
				// 如果是可自带套餐
				if (flag) {
					if (map1.get("isZd") != null && "0".equals(map1.get("isZd").toString())) {
						count++;
						break;
					}
				} else {
					break;
				}
			}
		}
		if (count <= 0) {
			orderYhPrice = Double.parseDouble(gsf) + Double.parseDouble(smfwf);
		}
		double xmFy = 0;
		for (Map<Object, Object> xmlMap : xmList) {
			if (xmlMap.get("isZd") != null && "1".equals(xmlMap.get("isZd").toString())) {
				BigDecimal bigDecimal = new BigDecimal(
						xmlMap.get("pjPrice") == null || "".equals(xmlMap.get("pjPrice").toString()) ? "0"
								: xmlMap.get("pjPrice").toString());
				BigDecimal bigDecimal1 = new BigDecimal(
						xmlMap.get("pjNum") == null || "".equals(xmlMap.get("pjNum").toString()) ? "0"
								: xmlMap.get("pjNum").toString());
				xmFy += bigDecimal.multiply(bigDecimal1).doubleValue();
			}
			xmFy += Double.parseDouble(xmlMap.get("xmprice") == null || "".equals(xmlMap.get("xmprice").toString())
					? "0" : xmlMap.get("xmprice").toString());
			xmlMap.put("wxxmid", "");
			xmlMap.put("orderId", orderId);
			xmlMap.put("createBy", jsId);
			jyOrderServiceImpl.insertJyOrderWxxm(xmlMap);
			jyOrderServiceImpl.insertJyOrderPj(xmlMap);
		}
		double orderPrice = xmFy + tcxmFy - tcjmFy + Double.parseDouble(gsf) + Double.parseDouble(smfwf) - orderYhPrice;
		map.put("orderYhPrice", orderYhPrice);
		map.put("orderPrice", orderPrice);
		jyOrderServiceImpl.updatePrice(map);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
		return outJson;
	}

	/**
	 * 
	 * startVideo:(开始录像). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/startVideo")
	@ResponseBody
	public String startVideo(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode");
		String time = request.getParameter("time");
		map.put("token", token);
		map.put("orderCode", orderCode);
		map.put("time", time);
		int count = jyOrderServiceImpl.updateWyStartTime(map);
		if (count == 1) {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
			return outJson;
		} else {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
			return outJson;
		}
	}

	/**
	 * 
	 * endVideo:(结束录像). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/endVideo")
	@ResponseBody
	public String endVideo(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode");
		String time = request.getParameter("time");
		map.put("token", token);
		map.put("orderCode", orderCode);
		map.put("time", time);
		int count = jyOrderServiceImpl.updateWyEndTime(map);
		if (count == 1) {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
			return outJson;
		} else {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
			return outJson;
		}
	}

	/**
	 * 
	 * getAllBgxm:(加载检测报告项目). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/getAllBgxm")
	@ResponseBody
	public String getAllBgxm(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		// 步骤一
		List<Map<String, Object>> list = jyOrderServiceImpl.getAllBgxm();
		// 步骤二
		List<Map<String, Object>> list2 = jyOrderServiceImpl.getAllBgxm2();
		Map<String, Object> mapStep1 = new HashMap<String, Object>();
		Map<String, Object> mapStep2 = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (list.size() > 0) {
			mapStep1.put("typeName", list.get(0).get("typeName"));
		} else {
			mapStep1.put("typeName", "");
		}
		mapStep1.put("list", list);

		if (list2.size() > 0) {
			mapStep2.put("typeName", list2.get(0).get("typeName"));
		} else {
			mapStep2.put("typeName", "");
		}
		mapStep2.put("list", list2);
		resultMap.put("step1", mapStep1);
		resultMap.put("step2", mapStep2);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), resultMap);
		return outJson;
	}

	/**
	 * 
	 * saveBgResult:(保存检测报告). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/saveBgResult")
	@ResponseBody
	public String saveBgResult(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		Integer jsId = technicianServiceImpl.getJSIdByToken(token);
		String orderCode = request.getParameter("orderCode");
		Integer orderId = jyOrderServiceImpl.getOrderIdByOrderCode(orderCode);
		String jsonStr = request.getParameter("jsonStr");
		JSONObject jsonObject = JSONObject.parseObject(jsonStr);
		JSONArray array = JSONArray.parseArray(jsonObject.getString("list"));
		ListIterator<Object> iterator = array.listIterator();
		while (iterator.hasNext()) {
			Map map2 = (Map) iterator.next();
			map2.put("orderId", orderId);
			map2.put("createBy", jsId);
			jyOrderServiceImpl.insertJyOrderwxbg(map2);
		}
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
		return outJson;
	}

	/**
	 * 
	 * getWxpic:(加载维修照片). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/getWxpic")
	@ResponseBody
	public String getWxpic(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode");
		List<Map<String, Object>> list = jyOrderServiceImpl.getWxpic(orderCode);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), list);
		return outJson;
	}

	/**
	 * 
	 * uploadWxpic:(上传维修照片). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/uploadWxpic")
	@ResponseBody
	public String uploadWxpic(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode");
		String base64 = request.getParameter("base64");
		String picName = request.getParameter("picName");
		Integer jsId = technicianServiceImpl.getJSIdByToken(token);
		map.put("orderCode", orderCode);
		map.put("base64", base64);
		map.put("picName", picName);
		map.put("createBy", jsId);
		jyOrderServiceImpl.insertJyBasePic(map);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
		return outJson;
	}

	/**
	 * 
	 * getPayList:(加载结算清单). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/getPayList")
	@ResponseBody
	public String getPayList(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode");
		String fzdjmf = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Map<String, Object>> tcList = jyOrderServiceImpl.getTcListByOrderCode(orderCode);
		List<Map<String, Object>> xmList = jyOrderServiceImpl.getXmListByOrderCode(orderCode);
		String gsf = jyOrderServiceImpl.getWxxmPrice("工时费");
		String smfwf = jyOrderServiceImpl.getWxxmPrice("上门服务费");
		String hj = jyOrderServiceImpl.getOrderHjByOrderCode(orderCode);
		List<String> list = new ArrayList<String>();
		boolean flag = false;
		for (Map<String, Object> tcMap : tcList) {
			if (tcMap.get("isBzdjm") != null && "0".equals(tcMap.get("isBzdjm").toString())) {
				if (tcMap.get("isZd") != null && "0".equals(tcMap.get("isZd").toString())) {
					flag = true;
					break;
				}
			}
		}
		for (Map<String, Object> tcMap : tcList) {
			if (!list.contains(tcMap.get("id").toString())) {
				list.add(tcMap.get("id").toString());
			}
		}
		List<Map<String, Object>> resultTcList = new ArrayList<Map<String, Object>>();
		for (String id : list) {
			Map<String, Object> cunMap = new HashMap<String, Object>();
			List<Map<String, Object>> tcxmList = new ArrayList<Map<String, Object>>();
			for (Map<String, Object> tcMap : tcList) {
				if (id.equals(tcMap.get("id").toString())) {
					Map<String, Object> tcxmMap = new HashMap<String, Object>();
					cunMap.put("tcName", tcMap.get("tcName"));
					cunMap.put("tcJmfy", tcMap.get("tcJmfy"));
					cunMap.put("tcPrice", tcMap.get("tcPrice"));
					tcxmMap.put("xmName", tcMap.get("xmName"));
					tcxmMap.put("xmprice", tcMap.get("xmprice"));
					tcxmMap.put("pjlb", tcMap.get("pjlb"));
					tcxmMap.put("pjCode", tcMap.get("pjCode"));
					tcxmMap.put("pjName", tcMap.get("pjName"));
					tcxmMap.put("pjpp", tcMap.get("pjpp"));
					tcxmMap.put("pjPrice", tcMap.get("pjPrice"));
					tcxmMap.put("pjNum", tcMap.get("pjNum"));
					tcxmMap.put("isZd", tcMap.get("isZd"));
					tcxmList.add(tcxmMap);
				}
			}
			cunMap.put("tcxmList", tcxmList);
			resultTcList.add(cunMap);
		}
		resultMap.put("tcList", resultTcList);
		resultMap.put("xmList", xmList);
		resultMap.put("gsf", gsf);
		resultMap.put("smfwf", smfwf);
		resultMap.put("hj", hj);
		if (flag) {
			fzdjmf = Double.parseDouble(gsf) + Double.parseDouble(smfwf) + "";
		} else {
			// fzdjmf = jyOrderServiceImpl.getOrderYhPrice(orderCode);
			fzdjmf = "0";
		}
		resultMap.put("fzdjmf", fzdjmf);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), resultMap);
		return outJson;
	}

	/**
	 * 
	 * checkCouponCode:(优惠券验证). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/checkCouponCode")
	@ResponseBody
	public String checkCouponCode(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String couponCode = request.getParameter("couponCode");
		String orderCode = request.getParameter("orderCode");
		String couponPrice = jyOrderServiceImpl.getCouponPrice(couponCode);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (couponPrice != null) {
			resultMap.put("couponPrice", couponPrice);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			String outJson = getOutJson1(statusCode, true, statusCode.toString(), resultMap);
			return outJson;
		} else {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_706;
			String outJson = getOutJson1(statusCode, false, statusCode.toString(), null);
			return outJson;
		}
	}

	/**
	 * 
	 * savePay:(确定支付). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/savePay")
	@ResponseBody
	public String savePay(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode");
		Integer orderId = jyOrderServiceImpl.getOrderIdByOrderCode(orderCode);
		String paySum = request.getParameter("paySum");
		String couponCode = request.getParameter("couponCode");
		String[] str = couponCode.split(",");
		String payType = request.getParameter("payType");
		String khqm = request.getParameter("khqm");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		map.put("orderCode", orderCode);
		map.put("paySum", paySum);
		map.put("couponCodes", couponCode);
		map.put("payType", payType);
		map.put("khqm", khqm);
		map.put("orderId", orderId);
		String price = "";
		int count = 0;
		// 失效优惠券编码
		String SXCouponCodes = "";
		for (String s : str) {
			map.put("couponCode", s);
			price = jyOrderServiceImpl.getCouponPrice(couponCode);
			if (price == null) {
				count++;
				SXCouponCodes += s + ",";
			}
		}
		if (count != 0) {
			resultMap.put("SXCouponCodes", SXCouponCodes.substring(0, SXCouponCodes.length() - 1));
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_706;
			String outJson = getOutJson1(statusCode, false, statusCode.toString(), resultMap);
			return outJson;
		}
		int row = jyOrderServiceImpl.updateOrderByPay(map);
		if (row == 1) {
			for (String s : str) {
				map.put("couponCode", s);
				jyOrderServiceImpl.updateCouponByCouponCode(map);
			}
		} else {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_707;
			String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
			return outJson;
		}
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
		return outJson;
	}

	/**
	 * 
	 * getSysTime:(同步服务器时间). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/getSysTime")
	@ResponseBody
	public String getSysTime(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/ss HH:mm:ss");
		String time = dateFormat.format(date);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("time", time);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), resultMap);
		return outJson;
	}

	/**
	 * 
	 * finished:(订单完成). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/finished")
	@ResponseBody
	public String finished(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		String token = request.getParameter("token");
		String orderCode = request.getParameter("orderCode");
		int row = jyOrderServiceImpl.updateJSFinish(orderCode);
		if (row == 1) {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
			return outJson;
		} else {
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			String outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
			return outJson;
		}
	}

	/**
	 * 
	 * checkPay:(确认支付（微信，支付宝扫码确认支付使用）). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param orderCode
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/checkPay")
	@ResponseBody
	public String checkPay(HttpServletRequest request, HttpServletResponse response, String orderCode,
			Map<String, Object> map) {
		String PayStatus = jyOrderServiceImpl.getPayStatusByOrderCode(orderCode);
		if("1".equals(PayStatus)){
			PayStatus="true";
		}
		if("0".equals(PayStatus)){
			PayStatus="false";
		}
		map.put("PayStatus", PayStatus);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson1(statusCode, true, statusCode.toString(), map);
		return outJson;
	}

}
